package com.example.cslp.controller;

import com.example.cslp.exception.handler.ServiceException;
import com.example.cslp.pojo.DTO.CourseDTO;
import com.example.cslp.service.IOssService;
import com.example.cslp.web.JsonResult;
import com.example.cslp.web.ServiceCode;
import com.sun.istack.NotNull;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ming
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/test")
public class OssController {

    @Autowired
    private IOssService iOssService;


    @PostMapping("/upload")
    public JsonResult fileUpload(@RequestParam("file") @NotNull MultipartFile multipartFile,@RequestPart @NonNull CourseDTO courseDTO) throws Exception {
        log.info("接收到新增课程视频请求，视频:{},课程信息:{}",multipartFile,courseDTO);
        if(multipartFile!=null && courseDTO!=null){
            iOssService.upload(multipartFile,courseDTO);
        }else {
            String msg="请传入视频或填入课程信息！";
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,msg);
        }
        return JsonResult.ok("上传视频成功");
    }
}
